package edu.pku.sei.modeler.editparts;

import java.beans.PropertyChangeEvent;
import java.util.List;

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.UnexecutableCommand;
import org.eclipse.gef.editpolicies.DirectEditPolicy;
import org.eclipse.gef.editpolicies.GraphicalNodeEditPolicy;
import org.eclipse.gef.requests.CreateConnectionRequest;
import org.eclipse.gef.requests.ReconnectRequest;
import org.eclipse.swt.graphics.Font;

import edu.pku.sei.modeler.commands.CreateLinkCommand;
import edu.pku.sei.modeler.figures.MetaCompoundNodeFigure;
import edu.pku.sei.modeler.images.ImageProvider;
import edu.pku.sei.modeler.model.core.CorePackage;
import edu.pku.sei.modeler.model.core.MetaAttribute;
import edu.pku.sei.modeler.model.core.MetaClass;
import edu.pku.sei.modeler.model.core.MetaElement;
import edu.pku.sei.modeler.model.core.MetaGeneralization;
import edu.pku.sei.modeler.model.core.MetaOperation;
import edu.pku.sei.modeler.model.core.MetaReference;
import edu.pku.sei.modeler.model.structure.MetaLink;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
/**
 * 
 * @author liangjie
 * @deprecated
 */
public class MetaCompoundNodeEditPart extends MetaNamedElementEditPart {
	private static Font ITALICS;
	public MetaCompoundNodeEditPart (MetaShapeElement model) {
		super(model);
	}
	public void activate() {
		super.activate();
		MetaClass model = (MetaClass)((MetaNode)getModel()).getModelElement();
		List list = model.getAttributes();
		for(int i=0; i<list.size(); i++) {
			MetaAttribute attr = (MetaAttribute)list.get(i);
			attr.addPropertyChangeListener(modelListener);
		}
	}

	public void deactivate() {
		MetaClass model = (MetaClass)((MetaNode)getModel()).getModelElement();
		List list = model.getAttributes();
		for(int i=0; i<list.size(); i++) {
			MetaAttribute attr = (MetaAttribute)list.get(i);
			attr.removePropertyChangeListener(modelListener);
		}
		super.deactivate();
	}
	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	protected IFigure createFigure() {
		Label header = new Label();
		MetaCompoundNodeFigure fig = new MetaCompoundNodeFigure(header);
		return fig;
	}

	protected DirectEditPolicy createDirectEditPolicy() {
		return null;
	}
	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
	 */
	protected void createEditPolicies() {
		super.createEditPolicies();
		installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE, new GraphicalNodeEditPolicy() {
			protected Command getConnectionCompleteCommand(CreateConnectionRequest request) {
				CreateLinkCommand cmd = (CreateLinkCommand)request.getStartCommand();
				cmd.setTarget((MetaNode)getHost().getModel());
				return cmd;
			}
			protected Command getConnectionCreateCommand(CreateConnectionRequest request) {
				MetaLink newObj = (MetaLink)request.getNewObject();
				MetaElement ele = newObj.getModelElement();
				if(ele instanceof MetaReference 
						|| ele instanceof MetaGeneralization) {
					Command cmd = new CreateLinkCommand(newObj,
							(MetaNode)getHost().getModel());
					request.setStartCommand(cmd);
					return cmd;
				}
				return null;
			}
			protected Command getReconnectSourceCommand(ReconnectRequest request) {
				return UnexecutableCommand.INSTANCE;
			}
			protected Command getReconnectTargetCommand(ReconnectRequest request) {
				return UnexecutableCommand.INSTANCE;
			}
		});
	}

	protected void handlePropertyChanged(PropertyChangeEvent event) {
		String propertyName = event.getPropertyName();
		if(propertyName.equals(CorePackage.CLASS_ABSTRACTFLAG)) {
			refreshVisuals();
		}
		else if(propertyName.equals(CorePackage.CLASS_ATTRIBUTES)) {
			refreshVisuals();
		}
		else if(propertyName.equals(CorePackage.CLASS_ABSTRACTFLAG)) {
			refreshVisuals();
		}
		else if(propertyName.equals(CorePackage.CLASS_GENERALIZATIONS)) {
			refreshVisuals();
		}
		else if(propertyName.equals(CorePackage.CLASS_INTERFACEFLAG)) {
			refreshVisuals();
		}
		else if(propertyName.equals(CorePackage.CLASS_OPERATIONS)) {
			refreshVisuals();
		}
		else if(propertyName.equals(CorePackage.CLASS_REFERENCES)) {
			refreshVisuals();
		}
		else if(propertyName.equals(CorePackage.CLASS_SPECIFIC)) {
			refreshVisuals();
		}
		// handler attribute's property change:
		else if(propertyName.equals(CorePackage.ATTRIBUTE_IDFLAG)) {
			refreshVisuals();
		}
		else if(propertyName.equals(CorePackage.TYPEDELEMENT_TYPE)) {
			refreshVisuals();
		}
		super.handlePropertyChanged(event);
	}
	private MetaClass getMetaClass() {
		return (MetaClass)((MetaNode)getModel()).getModelElement();
	}
	protected void refreshVisuals() {
		super.refreshVisuals();
		Label header = (Label)((MetaCompoundNodeFigure)getFigure()).getHeader();
		header.setText(getMetaClass().getName());
		Object obj = getModel();
		header.setIcon(ImageProvider.getImage(ImageProvider.RELATION_IMAGE));
		/*if (getMetaClass().isAbstract()) {
			if (ITALICS == null) {
				FontData[] fontDatas = header.getFont().getFontData();
				for (int i = 0; i < fontDatas.length; i++) {
					fontDatas[i].setStyle(fontDatas[i].getStyle() | SWT.ITALIC);
				}
				ITALICS = new Font(null, fontDatas);
			}
			header.setFont(ITALICS);
			getFigure().setBackgroundColor(new Color(null,230,230,230));
		}*/
		header.setFont(null);
		getFigure().setBackgroundColor(MetaCompoundNodeFigure.CLASS_COLOR);
		refreshAttributes();
		refreshOperations();
	}
	protected void refreshAttributes() {
		MetaCompoundNodeFigure fig = (MetaCompoundNodeFigure)this.getFigure();
		IFigure body = fig.getAttrCompart();
		MetaClass model = (MetaClass)((MetaShapeElement)getModel()).getModelElement();
		List v = model.getAttributes();
		((Figure)body).removeAll();
		for(int i=0; i<v.size(); i++)
		{
			Label attr = getAttributeFigure((MetaAttribute)v.get(i));
			body.add(attr);
		}
	}
	
	protected void refreshOperations() {
		MetaCompoundNodeFigure fig = (MetaCompoundNodeFigure)this.getFigure();
		MetaClass model = (MetaClass)((MetaShapeElement)getModel()).getModelElement();
		List v = model.getOperations();
		IFigure opCompart = fig.getOpCompart();
		((Figure)opCompart).removeAll();
		for(int i=0; i<v.size(); i++)
		{
			Label op = getOperationFigure((MetaOperation)v.get(i));
			opCompart.add(op);
		}
	}
	public Label getAttributeFigure(MetaAttribute attr) {
		Label fig = new Label();
		String type = "null";
		if(attr.getType()!=null) type = attr.getType().getName();
		fig.setText(attr.getName() + ":" + type);
//		fig.setIcon(UMLViewImages.getVisibilityImage(attr.getVisibility()));
		return fig;
	}
	public Label getOperationFigure(MetaOperation op) {
		Label fig = new Label();
		fig.setText(op.getName() + "()");
//		fig.setIcon(UMLViewImages.getVisibilityImage(op.getVisibility()));
		return fig;
	}
}
